﻿using System.Collections.Concurrent;
using static System.Console;

Title = "C#中的Queue和Stack";


Queue<string> q = new Queue<string>();
q.Enqueue("欢");
q.Enqueue("迎");
q.Enqueue("你");

foreach (var item in q)
{
    WriteLine(item);
}


Stack<int> stk = new Stack<int>();
stk.Push(1);
stk.Push(2);
stk.Push(3);

foreach (var item in stk)
{
    WriteLine(item);
}



//线程安全的队列 ConcurrentQueue 和栈 ConcurrentStack，多线程中使用。
int num = 1;
ConcurrentQueue<int> cq = new ConcurrentQueue<int>();

Task task = new Task(() =>
{
    for (int i = 0; i < 10; i++)
    {
        num += 1;
        cq.Enqueue(num);
        WriteLine("工作线程入队数据项：" + num);
    }
});
task.Start();

for (int i = 0; i < 10; i++)
{
    num += 1;
    cq.Enqueue(num);
    WriteLine("主线程入队数据：" + num);
}


int res = 0;
int count = cq.Count;
for(int i = 0; i < count; i++)
{
    bool b = cq.TryDequeue(out res) ;
    if (b)
    {
        WriteLine("出队列的数据是：" + res);
    }
}







ConcurrentStack<int> cs = new ConcurrentStack<int>();


Task tk = new Task(() =>
{
    for (int i = 0; i < 10; i++)
    {

    }
});
tk.Start();





ReadKey();